﻿using System;
using UnityEngine;

// Token: 0x02000396 RID: 918
public class TensileStrength : MonoBehaviour
{
	// Token: 0x060016B8 RID: 5816 RVA: 0x00097198 File Offset: 0x00095398
	protected virtual void Start()
	{
	}

	// Token: 0x060016B9 RID: 5817 RVA: 0x0009719C File Offset: 0x0009539C
	protected virtual void Update()
	{
		int num = 0;
		Vector3 vector = Vector3.zero;
		Vector3 a = Vector3.zero;
		for (int i = 0; i < this.rigids.Length; i++)
		{
			if (this.rigids[i].isKinematic)
			{
				num++;
				vector += this.rigids[i].position;
			}
			else
			{
				a += this.rigids[i].position;
			}
		}
		Vector3 vector2 = Vector3.zero;
		if (num == 0)
		{
			vector2 = a / (float)this.rigids.Length;
		}
		else if (num == this.rigids.Length)
		{
			vector2 = a / (float)(this.rigids.Length - num);
		}
		else
		{
			vector /= (float)num;
			a /= (float)(this.rigids.Length - num);
			vector2 = Vector3.Lerp(a, vector, this.kinematicWeight);
		}
		for (int j = 0; j < this.rigids.Length; j++)
		{
			if (!this.rigids[j].isKinematic)
			{
				Vector3 vector3 = this.rigids[j].position - vector2;
				if (vector3.magnitude < this.minDistance)
				{
					this.rigids[j].position = vector2 + vector3.normalized * this.minDistance;
					vector3 = this.rigids[j].position - vector2;
				}
				else if (vector3.magnitude > this.maxDistance)
				{
					this.rigids[j].position = vector2 + vector3.normalized * this.maxDistance;
					vector3 = this.rigids[j].position - vector2;
				}
				Vector3 a2 = vector2 + vector3.normalized * this.relaxDistance;
				Vector3 vector4 = a2 - this.rigids[j].position;
				vector4 *= this.strength;
				this.rigids[j].AddForce(vector4, ForceMode.Acceleration);
			}
		}
	}

	// Token: 0x04001A4E RID: 6734
	[SerializeField]
	protected float strength = 1f;

	// Token: 0x04001A4F RID: 6735
	[SerializeField]
	protected float relaxDistance = 0.1f;

	// Token: 0x04001A50 RID: 6736
	[SerializeField]
	protected float minDistance = 0.01f;

	// Token: 0x04001A51 RID: 6737
	[SerializeField]
	protected float maxDistance = 1f;

	// Token: 0x04001A52 RID: 6738
	[SerializeField]
	protected float kinematicWeight;

	// Token: 0x04001A53 RID: 6739
	[SerializeField]
	protected Rigidbody[] rigids;
}
